package com.example.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: xben
 * @Date: 2019/2/12 17:45
 */
@Component
public class ThrowExceptionFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(ThrowExceptionFilter.class);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        log.info("This is a pre filter,it will throw a RuntimeException");
        RequestContext ctx = RequestContext.getCurrentContext();
        doSomething();
        return null;
    }

    private void doSomething() throws ZuulException {
//        throw new ZuulException("查询服务异常",500,"校验密码错误");
        throw new ZuulRuntimeException(new ZuulException("查询服务异常", 500, "校验密码错误"));
    }
}
